<?php
	
	//拼团订单 不成功  24小时退款
	file_put_contents("dingshi.txt", date("Y-m-d H:i:s") . "执行定时任务！拼团24小时退款" . "\r\n", FILE_APPEND);
	$redis =connectRedis();
	$redis->set('back_order_list',null);
	$order_list = $redis->get('back_order_list');
	M('order')->where('refund_state=4 and refund_type=1')->setField('order_state','20');
	if($order_list==null){
		$rs=M('order')->field('order_id,order_sn,add_time,tuanselect,tuannum')->where("order_state='20' and tuanselect=1 and refund_state=0 and tuan_is_first_id='0'")->select();
		$newdata=array();
		if($rs){
			foreach($rs as $key=>$value){
				$num=get_tuan($value['order_id']);
				if($num<$value['tuannum']){
					$data['order_id']=$value['order_id'];
					$data['order']=$value['order_sn'];
					$data['add_time']=$value['add_time'];
					$data['neednum']=$value['tuannum'];
					$data['num']=$num;
					$data['clostime']=60*60*24;
					$newdata[]=$data;
				}
			}
		}
		
		//待处理订单
		if($newdata){
			
			
			$newdata=json_encode($newdata);
			$redis->set('back_order_list',$newdata);
			$order_list = $redis->get('back_order_list');
		}
	}
	
	if($order_list){
		$order_list=json_decode($order_list,true);
		foreach($order_list as $key=>$v){
			$time=time();
			$guoqi_time= (int)$v['add_time']+$v['clostime'];
		
			if($time>$guoqi_time){
			
				$o_info=M('order')->find($v['order_id']);
				if($o_info['order_state']=='20'&&$o_info['refund_state']==0){
					$o_data['order_id']=$v['order_id'];
					$o_data['refund_state']=4;//同意退款
					$o_data['refund_amount']=$o_info['order_amount'];//申请退款
					$o_data['refund_reason']='拼团不成功';//申请退款
					$o_data['refund_type']=1;//申请退款
					$o_data['refund_time']=time();//申请退款
					$o_data['refund_agreen_time']=time();//申请退款
					M('order')->save($o_data);
					
					
					$son_data['refund_state']=4;//同意退款
					$son_data['refund_amount']=$o_info['order_amount'];//申请退款
					$son_data['refund_reason']='拼团不成功';//申请退款
					$son_data['refund_type']=1;//申请退款
					$son_data['refund_time']=time();//申请退款
					$son_data['refund_agreen_time']=time();//申请退款
					$where['order_state']='20';
					$where['tuan_is_first_id']=$v['order_id'];
					M('order')->where($where)->save($son_data);
					unset($order_list[$key]);
				}
			
			}
		}
	
	}
	

	
?>